<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 * 系统会员相关类
 * 
 */
namespace osc\common\service;
use think\Db;
use think\Session;
use think\Loader;
//use SmsDemo;
class Rang{	

	

	

	function sendsms_common($mobile,$sess)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$mobile_code = $this->random(6,1);
		session($sess,strval($mobile_code)); 
		$smsusername = $mobile.$sess;
		session($smsusername,$mobile);
		$mobile_code=strval($mobile_code);
		$response = SmsDemo::sendSms($mobile,$sess,$mobile_code);
		 
		/*
		echo "发送短信(sendSms)接口返回的结果:\n";
		print_r($response);

		sleep(2);

		$response = SmsDemo::querySendDetails();
		echo "查询短信发送情况(querySendDetails)接口返回的结果:\n";
		print_r($response);
		*/
		//var_dump($response);
		return $response;
	}

	function sendsms_normal($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_normal($mobile);

		return $response;
	}

	function sendsms_delivery($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_delivery($mobile);

		return $response;
	}

	function sendsms_programme($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_programme($mobile);

		return $response;
	}

	function sendsms_contract($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_contract($mobile);

		return $response;
	}

	function sendsms_rang($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_rang($mobile);

		/*
		echo "发送短信(sendSms)接口返回的结果:\n";
		print_r($response);

		sleep(2);

		$response = SmsDemo::querySendDetails();
		echo "查询短信发送情况(querySendDetails)接口返回的结果:\n";
		print_r($response);
		*/
		//var_dump($response);
		return $response;
	}

	function sendsms_rang_nopass($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_rang_nopass($mobile);

		/*
		echo "发送短信(sendSms)接口返回的结果:\n";
		print_r($response);

		sleep(2);

		$response = SmsDemo::querySendDetails();
		echo "查询短信发送情况(querySendDetails)接口返回的结果:\n";
		print_r($response);
		*/
		//var_dump($response);
		return $response;
	}

	function sendsms_dlivery($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_dlivery($mobile);

		/*
		echo "发送短信(sendSms)接口返回的结果:\n";
		print_r($response);

		sleep(2);

		$response = SmsDemo::querySendDetails();
		echo "查询短信发送情况(querySendDetails)接口返回的结果:\n";
		print_r($response);
		*/
		//var_dump($response);
		return $response;
	}

	function sendsms_ticket_dlivery($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_ticket_dlivery($mobile);

		/*
		echo "发送短信(sendSms)接口返回的结果:\n";
		print_r($response);

		sleep(2);

		$response = SmsDemo::querySendDetails();
		echo "查询短信发送情况(querySendDetails)接口返回的结果:\n";
		print_r($response);
		*/
		//var_dump($response);
		return $response;
	}

	function sendsms_dlivery_nopass($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_dlivery_nopass($mobile);

		/*
		echo "发送短信(sendSms)接口返回的结果:\n";
		print_r($response);

		sleep(2);

		$response = SmsDemo::querySendDetails();
		echo "查询短信发送情况(querySendDetails)接口返回的结果:\n";
		print_r($response);
		*/
		//var_dump($response);
		return $response;
	}
	function sendsms_ticket_nopass($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_ticket_nopass($mobile);

		/*
		echo "发送短信(sendSms)接口返回的结果:\n";
		print_r($response);

		sleep(2);

		$response = SmsDemo::querySendDetails();
		echo "查询短信发送情况(querySendDetails)接口返回的结果:\n";
		print_r($response);
		*/
		//var_dump($response);
		return $response;
	}

	function sendsms_nopass($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_nopass($mobile);
		return $response;
	}

	function sendsms_exception($mobile)
	{
		set_time_limit(0);
		header('Content-Type: text/plain; charset=utf-8');
		$response = SmsDemo::sendsms_nopass($mobile);
		 
		return $response;
	}




	function random($length = 6 , $numeric = 0) {  
		PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);  
		if($numeric) {  
			$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));  
		} else {  
			$hash = '';  
			$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';  
			$max = strlen($chars) - 1;  
			for($i = 0; $i < $length; $i++) {  
				$hash .= $chars[mt_rand(0, $max)];  
			}  
		}  
		return $hash;  
	}


	  
	function percentEncode($str)  
	{  
		// 使用urlencode编码后，将"+","*","%7E"做替换即满足ECS API规定的编码规范  
		$res = urlencode($str);  
		$res = preg_replace('/\+/', '%20', $res);  
		$res = preg_replace('/\*/', '%2A', $res);  
		$res = preg_replace('/%7E/', '~', $res);  
		return $res;  
	}  
	  
	  
	function computeSignature($parameters, $accessKeySecret)  
	{  
		// 将参数Key按字典顺序排序  
		ksort($parameters);  
		// 生成规范化请求字符串  
		$canonicalizedQueryString = '';  
		foreach($parameters as $key => $value)  
		{  
		$canonicalizedQueryString .= '&' . $this->percentEncode($key)  
			. '=' . $this->percentEncode($value);  
		}  
		// 生成用于计算签名的字符串 stringToSign  
		$stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));  
		//echo "<br>".$stringToSign."<br>";  
		// 计算签名，注意accessKeySecret后面要加上字符'&'  
		$signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));  
		return $signature;  
	}  
	
    function do_get($app_key, $app_secret, $request_host, $request_uri, $request_method, $request_paras, &$info) {
		ksort($request_paras);
		$request_header_accept = "application/json;charset=utf-8";
		$content_type = "";
		$headers = array(
				'X-Ca-Key' => $app_key,
				'Accept' => $request_header_accept
				);
		ksort($headers);
		$header_str = "";
		$header_ignore_list = array('X-CA-SIGNATURE', 'X-CA-SIGNATURE-HEADERS', 'ACCEPT', 'CONTENT-MD5', 'CONTENT-TYPE', 'DATE');
		$sig_header = array();
		foreach($headers as $k => $v) {
			if(in_array(strtoupper($k), $header_ignore_list)) {
				continue;
			}
			$header_str .= $k . ':' . $v . "\n";
			array_push($sig_header, $k);
		}
		$url_str = $request_uri;
		$para_array = array();
		foreach($request_paras as $k => $v) {
			array_push($para_array, $k .'='. $v);
		}
		if(!empty($para_array)) {
			$url_str .= '?' . join('&', $para_array);
		}
		$content_md5 = "";
		$date = "";
		$sign_str = "";
		$sign_str .= $request_method ."\n";
		$sign_str .= $request_header_accept."\n";
		$sign_str .= $content_md5."\n";
		$sign_str .= "\n";
		$sign_str .= $date."\n";
		$sign_str .= $header_str;
		$sign_str .= $url_str;

		$sign = base64_encode(hash_hmac('sha256', $sign_str, $app_secret, true));
		$headers['X-Ca-Signature'] = $sign;
		$headers['X-Ca-Signature-Headers'] = join(',', $sig_header);
		$request_header = array();
		foreach($headers as $k => $v) {
			array_push($request_header, $k .': ' . $v);
		}

		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, $request_host . $url_str);
		//curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLINFO_HEADER_OUT, true);
		curl_setopt($ch, CURLOPT_VERBOSE, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $request_header);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$ret = curl_exec($ch);
		$info = curl_getinfo($ch);
		curl_close($ch);
		return $ret;
	}
	
	function https_request($url)  
	{  
		$curl = curl_init();  
		curl_setopt($curl, CURLOPT_URL, $url);  
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);  
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
		$data = curl_exec($curl);  
		if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}  
		curl_close($curl);  
		return $data;  
	}  
	function xml_to_array($xml){  
		$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";  
		if(preg_match_all($reg, $xml, $matches)){  
			$count = count($matches[0]);  
			for($i = 0; $i < $count; $i++){  
			$subxml= $matches[2][$i];  
			$key = $matches[1][$i];  
				if(preg_match( $reg, $subxml )){  
					$arr[$key] = $this->xml_to_array( $subxml );  
				}else{  
					$arr[$key] = $subxml;  
				}  
			}  
		}  
		return @$arr;  
	} 



	
}
